package com.qqtech.qquan.community.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseCostTypeDictEnum;
import com.qqtech.qquan.activity.enums.ActivityConst;
import com.qqtech.qquan.activity.model.ActivityBaseQuery;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.activity.service.ActivitySysDictService;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.enums.CommunityConst;
import com.qqtech.qquan.community.enums.CommunityMessageStatusDictEnum;
import com.qqtech.qquan.community.enums.CommunityMessageTypeDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageQuery;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.model.CommunitySysDict;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.community.service.CommunitySysDictService;
import com.qqtech.qquan.pub.model.PubConcern;
import com.qqtech.qquan.pub.model.PubPraise;
import com.qqtech.qquan.pub.service.PubCommentService;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.pub.service.PubPraiseService;
import com.qqtech.qquan.push.service.PushLogService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.enums.UserShieldTypeDictEnum;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserValue;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserShieldService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/community/message")
public class CommunityMessageController extends BaseWebController<CommunityMessage, CommunityMessageQuery> {
	@Resource
	private CommunityMessageService communityMessageService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private PubCommentService pubCommentService;

	@Resource
	private CommunitySysDictService communitySysDictService;

	@Resource
	private PubPraiseService pubPraiseService;

	@Resource
	private PubConcernService pubConcernService;

	@Resource
	private CircleMemberService circleMemberService;

	@Resource
	private CircleTopicService circleTopicService;

	@Resource
	private CircleBaseService circleBaseService;

	@Resource
	private PushLogService pushLogService;

	@Resource
	private ActivityBaseService activityBaseService;

	@Resource
	private ActivitySysDictService activitySysDictService;

	@Resource
	private UserShieldService userShieldService;

	@Resource
	private UserValueService userValueService;

	@Override
	protected BaseService<CommunityMessage> getBaseService() {
		return communityMessageService;
	}

	/**
	 * 分页
	 * 
	 * @param ommunityMessageQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody CommunityMessageQuery communityMessageQuery) {
		Result result = new Result();
		if (0 == communityMessageQuery.getType()) {// 全部
			communityMessageQuery.setType(null);
		}
		communityMessageQuery.setIsShow(YesNoEnum.YES.getCode());
		if (null != super.getUserId()) {
			List<Integer> shieldUserIds = userShieldService.getShieldUserIds(super.getUserId(),
					UserShieldTypeDictEnum.COMMUNITY_MESSAGE);
			if (null != shieldUserIds && shieldUserIds.size() > 0) {
				communityMessageQuery.setFilterUserIds(shieldUserIds);
			}
		}
		Pageable pageKit = new PageKit(communityMessageQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityMessageVo> page = communityMessageService.queryPageList(communityMessageQuery, pageKit);
		UserBaseVo userBaseVo = new UserBaseVo();
		String typeName = "";
		Integer commentCount = 0;// 评论数量
		for (CommunityMessageVo communityMessageVo : page) {
			communityMessageVo.setTypeName(typeName);
			communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
			if (communityMessageVo.isHasImg()) {
				communityMessageVo.setSource(communityMessageVo.getImgItems().get(0).getSource());
			}
			userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
			communityMessageVo.setUserLogo(userBaseVo.getFullLogo());
			communityMessageVo.setUserNick(userBaseVo.getNick());
			communityMessageVo.setIsShowPraiseList(false);
			commentCount = pubCommentService.getCommentCountByBizz(communityMessageVo.getId(),
					BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
			communityMessageVo.setReplyCount(commentCount);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param ommunityMessageQuery
	 * @return
	 */
	@RequestMapping(value = "/newPage")
	@ResponseBody
	public Result newPage(@RequestBody CommunityMessageQuery communityMessageQuery) {
		Result result = new Result();
		if (null != communityMessageQuery.getTagId() && -1 == communityMessageQuery.getTagId()) {
			communityMessageQuery.setTagId(null);
		}
		communityMessageQuery.setIsShow(YesNoEnum.YES.getCode());
		if (null != super.getUserId()) {
			if (communityMessageQuery.getFilterUserIds() == null
					|| communityMessageQuery.getFilterUserIds().isEmpty()) {
				List<Integer> shieldUserIds = userShieldService.getShieldUserIds(super.getUserId(),
						UserShieldTypeDictEnum.COMMUNITY_MESSAGE);
				if (null != shieldUserIds && shieldUserIds.size() > 0) {
					communityMessageQuery.setFilterUserIds(shieldUserIds);
				}
			}
			communityMessageQuery.setUserId(getUserId());
		} else {
			communityMessageQuery.setUserId(-1);
		}
		if (null == communityMessageQuery.getStartAt()) {
			communityMessageQuery.setStartAt(TimeUtil.now());
		}
		Pageable pageKit = new PageKit(communityMessageQuery.getPage(), new PageOrder());
		Page<CommunityMessageVo> page = communityMessageService.queryPageFriendUp(communityMessageQuery, pageKit);
		UserBaseVo userBaseVo = new UserBaseVo();
		UserValue userValue = new UserValue();
		for (CommunityMessageVo communityMessageVo : page) {
			communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
			userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
			userValue = userValueService.getById(communityMessageVo.getCreatorId());
			communityMessageVo.setHonorLevel(userValue.getHonorLevel());
			communityMessageVo.setHonorLevelName(userValue.getHonorLevelName());
			communityMessageVo.setUserLogo(userBaseVo.getFullLogo());
			communityMessageVo.setUserNick(userBaseVo.getNick());
			communityMessageVo.setIsPraised(YesNoEnum.NO.getCode());
			if (null != super.getUserId()) {
				if (pubPraiseService.isPraisedByUser(communityMessageVo.getId(),
						BizzLevel2Enum.COMMUNITY_MESSAGE.getCode(), super.getUserId())) {
					communityMessageVo.setIsPraised(YesNoEnum.YES.getCode());
				}
			}
			communityMessageVo.setShowOfficial(communityMessageVo.getIsOfficial() == 1 ? true : false);
			communityMessageVo.setShowTop(communityMessageVo.getIsTop() == 1 ? true : false);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param ommunityMessageQuery
	 * @return
	 */
	@RequestMapping(value = "/index")
	@ResponseBody
	public Result index(@RequestBody Map<String, String> map) {
		Result result = new Result();
		Integer commmunityId = Integer.valueOf(map.get("communityId"));
		// 活动
		List<ActivityBaseVo> activityList = getActivityList(commmunityId);
		result.put("activityList", activityList);
		// 八卦
		List<CommunityMessageVo> gossipList = getMessageList(commmunityId, CommunityMessageTypeDictEnum.GOSSIP);
		result.put("gossipList", gossipList);
		// 互助
		List<CommunityMessageVo> helpList = getMessageList(commmunityId, CommunityMessageTypeDictEnum.HELP);
		result.put("helpList", helpList);
		return result;
	}

	/**
	 * 新版大喇叭主页【列表】
	 * 
	 * @param ommunityMessageQuery
	 * @return
	 */
	@RequestMapping(value = "/indexV1")
	@ResponseBody
	public Result indexV1(@RequestBody CommunityMessageQuery messageQuery) {
		Result result = new Result();
		Integer commmunityId = messageQuery.getCommunityId();

		if (messageQuery.getPage() == 1) {
			List<ActivityBaseVo> activityList = getOneActivityList(commmunityId);
			result.put("activityList", activityList);// 活动列表

			List<CommunityMessageVo> messageList4Top = getMessageList4Top(messageQuery);
			result.put("messageList4Top", messageList4Top);// 大喇叭列表【置顶 ：包含官方的】
		}
		Page<CommunityMessageVo> messagePage4Normal = getMessagePage4Normal(messageQuery);
		result.put("messagePage4Normal", messagePage4Normal);// 大喇叭分页数据【非置顶的】
		return result;
	}

	/**
	 * 查询-大喇叭列表【置顶：包含官方的】
	 * 
	 * @param messageQuery
	 * @return
	 */
	private List<CommunityMessageVo> getMessageList4Top(CommunityMessageQuery messageQuery) {
		messageQuery.setIsShow(YesNoEnum.YES.getCode());
		messageQuery.setIsTop(YesNoEnum.YES.getCode());// 已置顶
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "topAt"));
		if (null != super.getUserId()) {
			if (messageQuery.getFilterUserIds() == null || messageQuery.getFilterUserIds().isEmpty()) {
				List<Integer> shieldUserIds = userShieldService.getShieldUserIds(super.getUserId(),
						UserShieldTypeDictEnum.COMMUNITY_MESSAGE);
				if (null != shieldUserIds && shieldUserIds.size() > 0) {
					messageQuery.setFilterUserIds(shieldUserIds);
				}
			}
		}
		List<CommunityMessageVo> messageList = communityMessageService.queryList(messageQuery, pageKit);
		UserBaseVo userBaseVo = null;
		Integer commentCount = 0;// 评论数量
		if (messageList != null && messageList.size() > 0) {
			for (CommunityMessageVo communityMessageVo : messageList) {
				communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
				if (communityMessageVo.isHasImg()) {
					communityMessageVo.setSource(communityMessageVo.getImgItems().get(0).getSource());
				}
				userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
				communityMessageVo.setUserLogo(userBaseVo.getFullLogo());
				communityMessageVo.setUserNick(userBaseVo.getNick());
				communityMessageVo.setIsShowPraiseList(false);
				commentCount = pubCommentService.getCommentCountByBizz(communityMessageVo.getId(),
						BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
				communityMessageVo.setReplyCount(commentCount);
			}
		}

		return messageList;
	}

	/**
	 * 分页查询-大喇叭分页数据【非置顶的】
	 * 
	 * @param messageQuery
	 * @return
	 */
	private Page<CommunityMessageVo> getMessagePage4Normal(CommunityMessageQuery messageQuery) {
		messageQuery.setIsShow(YesNoEnum.YES.getCode());
		messageQuery.setIsTop(YesNoEnum.NO.getCode());// 非置顶的
		Pageable pageKit = new PageKit(messageQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		if (null != super.getUserId()) {
			if (messageQuery.getFilterUserIds() == null || messageQuery.getFilterUserIds().isEmpty()) {
				List<Integer> shieldUserIds = userShieldService.getShieldUserIds(super.getUserId(),
						UserShieldTypeDictEnum.COMMUNITY_MESSAGE);
				if (null != shieldUserIds && shieldUserIds.size() > 0) {
					messageQuery.setFilterUserIds(shieldUserIds);
				}
			}
		}
		Page<CommunityMessageVo> messagePage = communityMessageService.queryPageList(messageQuery, pageKit);
		UserBaseVo userBaseVo = null;
		Integer commentCount = 0;// 评论数量
		List<CommunityMessageVo> messageList = messagePage.getContent();
		if (messageList != null && messageList.size() > 0) {
			for (CommunityMessageVo communityMessageVo : messageList) {
				communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
				if (communityMessageVo.isHasImg()) {
					communityMessageVo.setSource(communityMessageVo.getImgItems().get(0).getSource());
				}
				userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
				communityMessageVo.setUserLogo(userBaseVo.getFullLogo());
				communityMessageVo.setUserNick(userBaseVo.getNick());
				communityMessageVo.setIsShowPraiseList(false);
				commentCount = pubCommentService.getCommentCountByBizz(communityMessageVo.getId(),
						BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
				communityMessageVo.setReplyCount(commentCount);
			}
		}
		return messagePage;
	}

	private List<CommunityMessageVo> getMessageList(Integer commmunityId,
			CommunityMessageTypeDictEnum communityMessageTypeDictEnum) {
		CommunityMessageQuery communityMessageQuery = new CommunityMessageQuery();
		communityMessageQuery.setCommunityId(commmunityId);
		communityMessageQuery.setType(communityMessageTypeDictEnum.getCode());
		communityMessageQuery.setIsShow(YesNoEnum.YES.getCode());
		if (null != super.getUserId()) {
			List<Integer> shieldUserIds = userShieldService.getShieldUserIds(super.getUserId(),
					UserShieldTypeDictEnum.COMMUNITY_MESSAGE);
			if (null != shieldUserIds && shieldUserIds.size() > 0) {
				communityMessageQuery.setFilterUserIds(shieldUserIds);
			}
		}
		Pageable pageKit = new PageKit(1, 3, new PageOrder(Direction.DESC, "id"));
		List<CommunityMessageVo> communityMessageList = communityMessageService.queryList(communityMessageQuery,
				pageKit);
		UserBaseVo userBaseVo = new UserBaseVo();
		String typeName = "";
		Integer commentCount = 0;// 评论数量
		for (CommunityMessageVo communityMessageVo : communityMessageList) {
			communityMessageVo.setTypeName(typeName);
			communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
			if (communityMessageVo.isHasImg()) {
				communityMessageVo.setSource(communityMessageVo.getImgItems().get(0).getSource());
			}
			userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
			communityMessageVo.setUserLogo(userBaseVo.getFullLogo());
			communityMessageVo.setUserNick(userBaseVo.getNick());
			communityMessageVo.setIsShowPraiseList(false);
			commentCount = pubCommentService.getCommentCountByBizz(communityMessageVo.getId(),
					BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
			communityMessageVo.setReplyCount(commentCount);
		}
		return communityMessageList;
	}

	private List<ActivityBaseVo> getActivityList(Integer commmunityId) {
		ActivityBaseQuery activityBaseQuery = new ActivityBaseQuery();
		activityBaseQuery.setBizzId(commmunityId);
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, 3, new PageOrder(Direction.ASC, "status"),
				new PageOrder(Direction.DESC, "id"));
		List<ActivityBaseVo> activityList = activityBaseService.queryList(activityBaseQuery, pageKit);
		String typeName = "";
		String statusName = "";
		Integer commentCount = 0;// 评论数量
		for (ActivityBaseVo activityBaseVo : activityList) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			statusName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_STATUS,
					String.valueOf(activityBaseVo.getStatus()));
			activityBaseVo.setShowStatus(statusName);
			activityBaseVo.setIsShowPraiseList(false);
			commentCount = pubCommentService.getCommentCountByBizz(activityBaseVo.getId(),
					BizzLevel1Enum.ACTIVITY.getCode());
			activityBaseVo.setReplyCount(commentCount);
		}
		return activityList;
	}

	/**
	 * 查询 最新活动列表【只有一个活动】
	 * 
	 * @param commmunityId
	 * @return
	 */
	private List<ActivityBaseVo> getOneActivityList(Integer commmunityId) {
		ActivityBaseQuery activityBaseQuery = new ActivityBaseQuery();
		activityBaseQuery.setBizzId(commmunityId);
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, 1, new PageOrder(Direction.ASC, "status"),
				new PageOrder(Direction.DESC, "id"));
		List<ActivityBaseVo> activityList = activityBaseService.queryList(activityBaseQuery, pageKit);
		String typeName = "";
		String statusName = "";
		Integer commentCount = 0;// 评论数量
		for (ActivityBaseVo activityBaseVo : activityList) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			statusName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_STATUS,
					String.valueOf(activityBaseVo.getStatus()));
			activityBaseVo.setShowStatus(statusName);
			activityBaseVo.setIsShowPraiseList(false);
			commentCount = pubCommentService.getCommentCountByBizz(activityBaseVo.getId(),
					BizzLevel1Enum.ACTIVITY.getCode());
			activityBaseVo.setReplyCount(commentCount);
		}
		return activityList;
	}

	/**
	 * 分页
	 * 
	 * @param ommunityMessageQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody CommunityMessageQuery communityMessageQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		UserBaseVo curUser = userBaseService.getById(curUserId);
		String curUserLogo = curUser.getLogo();
		String curUserName = curUser.getNick();
		communityMessageQuery.setCreatorId(curUserId);
		communityMessageQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(communityMessageQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityMessageVo> page = communityMessageService.queryPageList(communityMessageQuery, pageKit);
		CommunityBaseVo communityBaseVo = new CommunityBaseVo();
		Integer commentCount = 0;// 评论数量
		for (CommunityMessageVo communityMessageVo : page) {
			communityBaseVo = communityBaseService.getById(communityMessageVo.getCommunityId());
			communityMessageVo.setCommunityName(communityBaseVo.getName());
			communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
			if (communityMessageVo.isHasImg()) {
				communityMessageVo.setSource(communityMessageVo.getImgItems().get(0).getSource());
			}
			commentCount = pubCommentService.getCommentCountByBizz(communityMessageVo.getId(),
					BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
			communityMessageVo.setReplyCount(commentCount);
			communityMessageVo.setCreatorName(curUserName);
			communityMessageVo.setCreatorImg(curUserLogo);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * pv+1再获取详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody CommunityMessageQuery communityMessageQuery) {
		Result result = new Result();
		CommunityMessageVo detailVo = communityMessageService.handleDetail(communityMessageQuery.getId());
		UserBaseVo userBaseVo = userBaseService.getById(detailVo.getCreatorId());
		detailVo.setUserLogo(userBaseVo.getFullLogo());
		detailVo.setUserNick(userBaseVo.getNick());
		detailVo.setImgString(detailVo.getImg(), SysConst.IMG_POSTFIX);
		if (detailVo.isHasImg()) {
			detailVo.setSource(detailVo.getImgItems().get(0).getSource());
		}
		detailVo.setIsPraised(YesNoEnum.NO.getCode());
		if (!super.isVisitor()) {
			// 我是否已点赞
			if (pubPraiseService.isPraisedByUser(detailVo.getId(), BizzLevel2Enum.COMMUNITY_MESSAGE.getCode(),
					super.getUserId())) {
				detailVo.setIsPraised(YesNoEnum.YES.getCode());
			}
		}
		addConcernInfo(detailVo);
		detailVo.setIsShowPraiseList(true);
		addPraiseUserList(detailVo);
		result.put("detailVo", detailVo);
		return result;
	}

	/**
	 * toUpdate
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	@ResponseBody
	public Result toUpdate(@RequestBody CommunityMessageQuery communityMessageQuery) {
		Result result = new Result();
		CommunityMessageVo communityMessageVo = communityMessageService.getById(communityMessageQuery.getId());
		CommunityBaseVo communityBaseVo = communityBaseService.getById(communityMessageVo.getCommunityId());
		communityMessageVo.setCommunityName(communityBaseVo.getName());
		result.put("communityMessageVo", communityMessageVo);
		return result;
	}

	/**
	 * 更改
	 * 
	 * @param communityMessage
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(@RequestBody CommunityMessage communityMessage) {
		communityMessage.setImg(communityMessage.getImg().replaceAll(communityMessage.getFileDomain(), ""));
		Result result = new Result();
		communityMessageService.updateById(communityMessage);
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param communityMessage
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody CommunityMessageVo communityMessageVo) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		UserBaseVo curUser = userBaseService.getById(curUserId);
		if (null == curUser.getCommunityId()) {
			result.addErr("请加入社区在发布邻里动态。");
			return result;
		}
		communityMessageVo.setCommunityId(curUser.getCommunityId());
		communityMessageVo.setIsOfficial(YesNoEnum.NO.getCode());
		communityMessageVo.setIsTop(YesNoEnum.NO.getCode());
		if (curUser != null) {
			// 如果是官方用户发布的大喇叭，就【官方并置顶】
			if (curUser.getIsOfficial() == YesNoEnum.YES.getCode()) {
				communityMessageVo.setIsOfficial(YesNoEnum.YES.getCode());
				communityMessageVo.setIsTop(YesNoEnum.YES.getCode());
				communityMessageVo.setTopAt(TimeUtil.now());
			}
		}
		communityMessageVo.setCreatorId(curUserId);
		communityMessageVo.setPv(0);
		communityMessageVo.setStatus(CommunityMessageStatusDictEnum.NORMAL.getCode());
		communityMessageVo.setIsShow(YesNoEnum.YES.getCode());
		communityMessageService.handleAdd(communityMessageVo);
		result.put("communityId", curUser.getCommunityId());
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody CommunityMessage communityMessage) {
		Result result = new Result();
		CommunityMessage delVo = new CommunityMessage();
		delVo.setId(communityMessage.getId());
		delVo.setIsShow(YesNoEnum.NO.getCode());
		delVo.setStatus(CommunityMessageStatusDictEnum.DELETE.getCode());
		communityMessageService.updateById(delVo);
		return result;
	}

	/**
	 * 标签列表
	 */
	@RequestMapping(value = "/tagList")
	@ResponseBody
	public Result circleTypeList() {
		Result result = new Result();
		List<CommunitySysDict> tagList = communitySysDictService.get(CommunityConst.DICTYPE_COMMUNITY_MESSAGE_TAG);
		result.put("tagList", tagList);
		return result;
	}

	/**
	 * 查询一个话题的点赞人列表
	 * 
	 * @param messageQuery
	 * @return
	 */
	@RequestMapping(value = "/getPraiseUserList")
	@ResponseBody
	public Result getPraiseUserList(@RequestBody CommunityMessageQuery messageQuery) {
		Result result = new Result();
		List<PubPraise> praiseList = pubPraiseService.queryPraiseListByBizz(messageQuery.getId(),
				BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
		List<UserBase> userList4Praise = Lists.newArrayList();
		StringBuffer sb = new StringBuffer();
		if (praiseList != null && praiseList.size() > 0) {
			for (int i = 0; i < praiseList.size(); i++) {
				PubPraise praise = praiseList.get(i);
				UserBase user = userBaseService.getById(praise.getCreatorId());
				if (user != null) {
					userList4Praise.add(user);
					if (i == praiseList.size() - 1) {
						sb.append("<a href='#' style='text-decoration: none' id='");
						sb.append(user.getId());
						sb.append("'>");
						sb.append(user.getNick());
						sb.append("</a>");
					} else {
						sb.append("<a href='#'  style='text-decoration: none' id='");
						sb.append(user.getId());
						sb.append("'>");
						sb.append(user.getNick());
						sb.append("</a>");
						sb.append("，");
					}
				}
			}
		}
		result.put("praiseUserList", sb.toString());
		result.put("userList4Praise", userList4Praise);
		return result;
	}

	// 附加 关注信息
	private void addConcernInfo(CommunityMessageVo communityMessageVo) {
		PubConcern pubConcern = new PubConcern();
		if (!super.isVisitor()) {
			Integer curUserId = super.getUserId();
			pubConcern.setCreatorId(curUserId);
			pubConcern.setBizzId(communityMessageVo.getId());
			pubConcern.setBizzType(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
			PubConcern pc = pubConcernService.getOne(pubConcern);
			if (pc == null) {
				communityMessageVo.setIsConcern(YesNoEnum.NO.getCode());
			} else {
				communityMessageVo.setIsConcern(YesNoEnum.YES.getCode());
			}
		} else {
			communityMessageVo.setIsConcern(YesNoEnum.NO.getCode());
		}
	}

	// 附加点赞人列表
	private void addPraiseUserList(CommunityMessageVo communityMessageVo) {
		List<PubPraise> praiseList = pubPraiseService.queryPraiseListByBizz(communityMessageVo.getId(),
				BizzLevel2Enum.COMMUNITY_MESSAGE.getCode());
		StringBuffer sb = new StringBuffer();
		UserBase user = null;
		PubPraise praise = null;
		List<UserBase> userList4Praise = Lists.newArrayList();
		for (int i = 0; i < praiseList.size(); i++) {
			praise = praiseList.get(i);
			user = userBaseService.getById(praise.getCreatorId());
			if (user != null) {
				userList4Praise.add(user);
				if (i == praiseList.size() - 1) {
					sb.append("<a href='#' style='text-decoration: none' id='");
					sb.append(user.getId());
					sb.append("'>");
					sb.append(user.getNick());
					sb.append("</a>");
				} else {
					sb.append("<a href='#'  style='text-decoration: none' id='");
					sb.append(user.getId());
					sb.append("'>");
					sb.append(user.getNick());
					sb.append("</a>");
					sb.append("，");
				}
			}

		}
		communityMessageVo.setPraiseUserList(sb.toString());// 点赞人列表string
		communityMessageVo.setUserList4Praise(userList4Praise);// 点赞人对象列表
	}

	/**
	 * 人个中心--分页查询她在一个社区发布的动态
	 * 
	 * @param messageQuery【creatorId,communityId】
	 * @return
	 * @author woon
	 */
	@RequestMapping(value = "/list4HerInCommunity")
	@ResponseBody
	public Result list4HerInCommunity(@RequestBody CommunityMessageQuery messageQuery) {
		Result result = new Result();
		Integer creatorId = messageQuery.getCreatorId();
		if (creatorId == null) {
			result.addErr("用户id为空");
			return result;
		}
		messageQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(messageQuery.getPage(), new PageOrder(Direction.ASC, "isOfficial"),
				new PageOrder(Direction.ASC, "isTop"), new PageOrder(Direction.DESC, "id"));
		Page<CommunityMessageVo> page = communityMessageService.queryPageList(messageQuery, pageKit);
		List<CommunityMessageVo> communityMessageList = page.getContent();
		if (communityMessageList != null && communityMessageList.size() > 0) {
			UserBaseVo creatorVo = userBaseService.getById(creatorId);// 用于取用户呢称、头像
			for (CommunityMessageVo communityMessageVo : communityMessageList) {
				if (creatorVo != null) {
					communityMessageVo.setUserLogo(creatorVo.getFullLogo());
					communityMessageVo.setUserNick(creatorVo.getNick());
				}
				communityMessageVo.setImgString(communityMessageVo.getImg(), SysConst.IMG_POSTFIX);
				if (pubPraiseService.isPraisedByUser(communityMessageVo.getId(),
						BizzLevel2Enum.COMMUNITY_MESSAGE.getCode(), super.getUserId())) {
					communityMessageVo.setIsPraised(YesNoEnum.YES.getCode());
				}
				communityMessageVo.setShowOfficial(communityMessageVo.getIsOfficial() == 1 ? true : false);
				communityMessageVo.setShowTop(communityMessageVo.getIsTop() == 1 ? true : false);
			}
		}
		result.put("messagePage", page);
		return result;
	}
}
